package com.inji.spark.biz.util;

import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;

import javax.sql.DataSource;

import com.alibaba.druid.pool.DruidDataSourceFactory;

/**
 * @author liukz
 *
 */
public class JdbcUtils {
	public static Connection conn = null;

	public static Connection getConnectionDruid() throws Exception {
		if (conn != null) {
			return conn;
		}

		try {
			Properties prop = new Properties();
			InputStream in = JdbcUtils.class.getClassLoader()
					.getResourceAsStream("druid.properties");
			prop.load(in);
			DataSource dataSource = DruidDataSourceFactory
					.createDataSource(prop);
			conn = dataSource.getConnection();

		} catch (Exception e) {
			e.printStackTrace();
		}

		return conn;
	}
	
	public static Connection getConnectionJdbc() throws Exception {
		if (conn != null) {
			return conn;
		}

		try {
			Properties prop = new Properties();
			InputStream in = JdbcUtils.class.getClassLoader()
					.getResourceAsStream("druid.properties");
            prop.load(in);
            Class.forName(prop.getProperty("driverClassName"));
            conn = DriverManager.getConnection(prop.getProperty("url"),prop.getProperty("user"),prop.getProperty("password"));
		} catch (Exception e) {
			e.printStackTrace();
		}

		return conn;
	}
	public static void main(String[] args) throws Exception {
		Connection connection = JdbcUtils.getConnectionJdbc();
		System.out.println(connection);
	}
}
